package org.bbs.demo.concern.api;


import org.bbs.demo.advice.BusinessException;
import org.bbs.demo.common.BBSConstants;
import org.bbs.demo.common.Result;
import org.bbs.demo.concern.dao.ShowConcernDao;
import org.bbs.demo.concern.dto.InfoDto;
import org.bbs.demo.concern.dto.ShowDto;
import org.bbs.demo.utils.jwtutil.JWTutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/concern")
@Transactional //事务声明，表示该类的所有方法都是事务性的，即方法中的所有访问数据库操作构成了一个整体事务，具有原子性
public class ShowConcernApi {
    @Autowired
    private ShowConcernDao showconcerndao;

    @PostMapping("/show/concernList")
    public Result info(@RequestBody ShowDto showdto, @RequestHeader String Token){
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<InfoDto> userList = new ArrayList<InfoDto>();

        String userId;
        if(showdto.getUser_id() == null){
            userId = JWTutil.getUserID(Token);
            showdto.setUser_id(userId);
        }

        if(showconcerndao.findUser(showdto.getUser_id()) == null){
            throw new BusinessException("用户id不存在");
            //return Result.err(BBSConstants.CODE_ERR_BUSINESS,"用户id不存在");
        }

        if(showdto.isSpecial()){
            list.addAll(showconcerndao.findSpecial(showdto.getUser_id()));
        }
        else{
            list.addAll(showconcerndao.findConcern(showdto.getUser_id()));
        }

        for(int i=0;i< list.size();i++){
            userList.add(showconcerndao.getUserInfo(list.get(i)));
        }
        return Result.OK("获取成功",userList);
    }
}
